1 package edu.jiangxin.apktoolbox.convert.relationship; 2 3 import edu.jiangxin.apktoolbox.swing.extend.EasyPanel; 4 import org.apache.commons.lang3.StringUtils; 5 6 import javax.swing.*; 7 import java.awt.*; 8 import java.awt.event.ActionEvent; 9 import java.awt.event.ActionListener; 10 import java.util.HashMap; 11 import java.util.Map; 12 import java.util.Stack; 13 14 public class RelationShipConvertPanel extends EasyPanel { 15 private static final Map<String, Map<String, String>> dataMap = new HashMap<>(); 16 17 private String now = "你"; 18 19 private JTextField inputTextField; 20 private JTextField outputTextField; 21 22 private final Stack<String> stack = new Stack<>(); 23 24 static { 25 dataMap.put("不存在", Map.of("父", "不存在", "母", "不存在", "兄", "不存在", "弟", "不存在", "姐", "不存在", "妹", "不存在", "夫", "不存在", "妻", "不存在", "儿", "不存在", "女", "不存在")); 26 dataMap.put("你", Map.of("父", "爸爸", "母", "妈妈", "兄", "哥哥", "弟", "弟弟", "姐", "姐姐", "妹", "妹妹", "夫", "老公", "妻", "老婆", "儿", "儿子", "女", "女儿")); 27 dataMap.put("爸爸", Map.of("父", "爷爷", "母", "奶奶", "兄", "伯伯", "弟", "叔叔", "姐", "姑姑", "妹", "姑姑", "夫", "不存在", "妻", "妈妈", "儿", "<哥哥><弟弟>", "女", "<姐姐><妹妹>")); 28 dataMap.put("妈妈", Map.of("父", "姥爷", "母", "姥姥", "兄", "舅舅", "弟", "舅舅", "姐", "大姨", "妹", "小姨", "夫", "爸爸", "妻", "不存在", "儿", "<哥哥><弟弟>", "女", "<姐姐><妹妹>")); 29 dataMap.put("哥哥", Map.of("父", "爸爸", "母", "妈妈", "兄", "哥哥", "弟", "<哥哥><弟弟>", "姐", "姐姐", "妹", "<姐姐><妹妹>", "夫", "不存在", "妻", "嫂子", "儿", "侄子", "女", "侄女")); 30 dataMap.put("弟弟", Map.of("父", "爸爸", "母", "妈妈", "兄", "<哥哥><弟弟>", "弟", "弟弟", "姐", "<姐姐><妹妹>", "妹", "妹妹", "夫", "不存在", "妻", "弟媳", "儿", "侄子", "女", "侄女")); 31 dataMap.put("姐姐", Map.of("父", "爸爸", "母", "妈妈", "兄", "哥哥", "弟", "<哥哥><弟弟>", "姐", "姐姐", "妹", "<姐姐><妹妹>", "夫", "姐夫", "妻", "不存在", "儿", "外甥", "女", "外甥女")); 32 dataMap.put("妹妹", Map.of("父", "爸爸", "母", "妈妈", "兄", "<哥哥><弟弟>", "弟", "弟弟", "姐", "<姐姐><妹妹>", "妹", "妹妹", "夫", "妹夫", "妻", "不存在", "儿", "外甥", "女", "外甥女")); 33 dataMap.put("老婆", Map.of("父", "岳父", "母", "岳母", "兄", "大舅", "弟", "小舅", "姐", "大姨", "妹", "小姨", "夫", "你", "妻", "不存在", "儿", "儿子", "女", "女儿")); 34 dataMap.put("儿子", Map.of("父", "你", "母", "老婆", "兄", "儿子", "弟", "儿子", "姐", "女儿", "妹", "女儿", "夫", "不存在", "妻", "儿媳", "儿", "孙子", "女", "孙女")); 35 dataMap.put("女儿", Map.of("父", "你", "母", "老婆", "兄", "儿子", "弟", "儿子", "姐", "女儿", "妹", "女儿", "夫", "女婿", "妻", "不存在", "儿", "外孙", "女", "外孙女")); 36 dataMap.put("伯伯", Map.of("父", "爷爷", "母", "奶奶", "兄", "伯伯", "弟", "<伯伯><叔叔>", "姐", "姑姑", "妹", "姑姑", "夫", "不存在", "妻", "婶婶", "儿", "<哥哥><弟弟>", "女", "<姐姐><妹妹>")); 37 dataMap.put("叔叔", Map.of("父", "爷爷", "母", "奶奶", "兄", "<伯伯><叔叔>", "弟", "叔叔", "姐", "姑姑", "妹", "姑姑", "夫", "不存在", "妻", "婶婶", "儿", "<哥哥><弟弟>", "女", "<姐姐><妹妹>")); 38 dataMap.put("姑姑", Map.of("父", "爷爷", "母", "奶奶", "兄", "<伯伯><叔叔>", "弟", "<伯伯><叔叔>", "姐", "姑姑", "妹", "姑姑", "夫", "姑父", "妻", "不存在", "儿", "<哥哥><弟弟>", "女", "<姐姐><妹妹>")); 39 dataMap.put("岳父", Map.of("父", "爷爷", "母", "奶奶", "兄", "伯伯", "弟", "叔叔", "姐", "姑姑", "妹", "姑姑", "夫", "不存在", "妻", "岳母", "儿", "<哥哥><弟弟>", "女", "<姐姐><妹妹>")); 40 dataMap.put("岳母", Map.of("父", "姥爷", "母", "姥姥", "兄", "舅舅", "弟", "舅舅", "姐", "大姨", "妹", "小姨", "夫", "岳父", "妻", "不存在", "儿", "<哥哥><弟弟>", "女", "<姐姐><妹妹>")); 41 } 42 43 @Override 44 public void initUI() { 45 setLayout(new GridBagLayout()); 46 GridBagConstraints gbc = new GridBagConstraints(); 47 48 inputTextField = new MyTextField(); 49 50 gbc.insets = new Insets(5, 5, 5, 5); 51 gbc.gridx = 0; 52 gbc.gridy = 0; 53 gbc.gridwidth = 4; 54 gbc.fill = GridBagConstraints.HORIZONTAL; 55 add(inputTextField, gbc); 56 57 outputTextField = new MyTextField(); 58 outputTextField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 59 60 gbc.gridy = 1; 61 add(outputTextField, gbc); 62 63 ButtonListener btnListener = new ButtonListener(); 64 65 JButton fatherButton = new MyButton("父"); 66 fatherButton.addActionListener(btnListener); 67 68 gbc.gridx = 0; 69 gbc.gridy = 2; 70 gbc.gridwidth = 1; 71 add(fatherButton, gbc); 72 73 JButton motherButton = new MyButton("母"); 74 motherButton.addActionListener(btnListener); 75 76 gbc.gridx = 1; 77 add(motherButton, gbc); 78 79 JButton backButton = new MyButton("←"); 80 backButton.addActionListener(btnListener); 81 82 gbc.gridx = 2; 83 add(backButton, gbc); 84 85 JButton acButton = new MyButton("AC"); 86 acButton.addActionListener(btnListener); 87 88 gbc.gridx = 3; 89 add(acButton, gbc); 90 91 JButton brotherButton = new MyButton("兄"); 92 brotherButton.addActionListener(btnListener); 93 94 gbc.gridx = 0; 95 gbc.gridy = 3; 96 add(brotherButton, gbc); 97 98 JButton littleBrotherButton = new MyButton("弟"); 99 littleBrotherButton.addActionListener(btnListener); 100 101 gbc.gridx = 1; 102 add(littleBrotherButton, gbc); 103 104 JButton husbandButton = new MyButton("夫"); 105 husbandButton.addActionListener(btnListener); 106 107 gbc.gridx = 2; 108 add(husbandButton, gbc); 109 110 JButton obligateButton = new MyButton(" "); 111 obligateButton.setEnabled(false); 112 113 gbc.gridx = 3; 114 add(obligateButton, gbc); 115 116 JButton sisterButton = new MyButton("姐"); 117 sisterButton.addActionListener(btnListener); 118 119 gbc.gridx = 0; 120 gbc.gridy = 4; 121 add(sisterButton, gbc); 122 123 JButton littleSisterButton = new MyButton("妹"); 124 littleSisterButton.addActionListener(btnListener); 125 126 gbc.gridx = 1; 127 add(littleSisterButton, gbc); 128 129 JButton wifeButton = new MyButton("妻"); 130 wifeButton.addActionListener(btnListener); 131 132 gbc.gridx = 2; 133 add(wifeButton, gbc); 134 135 JButton deButton = new MyButton("的"); 136 deButton.addActionListener(btnListener); 137 138 gbc.gridx = 3; 139 add(deButton, gbc); 140 141 JButton sonButton = new MyButton("儿"); 142 sonButton.addActionListener(btnListener); 143 144 gbc.gridx = 0; 145 gbc.gridy = 5; 146 add(sonButton, gbc); 147 148 JButton daughterbButton = new MyButton("女"); 149 daughterbButton.addActionListener(btnListener); 150 151 gbc.gridx = 1; 152 add(daughterbButton, gbc); 153 154 JButton resultButton = new MyButton("="); 155 resultButton.addActionListener(btnListener); 156 157 gbc.gridx = 2; 158 gbc.gridwidth = 2; 159 add(resultButton, gbc); 160 } 161 162 static class MyTextField extends JTextField { 163 public MyTextField() { 164 super(); 165 Font font = new Font("宋体", Font.BOLD, 15); 166 setFont(font); 167 setEditable(false); 168 } 169 } 170 171 static class MyButton extends JButton { 172 public MyButton(String text) { 173 super(text); 174 Font font = new Font("宋体", Font.BOLD, 30); 175 setFont(font); 176 } 177 } 178 179 class ButtonListener implements ActionListener { 180 public void actionPerformed(ActionEvent e) { 181 String which = ((JButton) e.getSource()).getText(); 182 switch (which) { 183 case "AC" -> { 184 now = "你"; 185 inputTextField.setText(""); 186 outputTextField.setText(""); 187 } 188 case "=" -> outputTextField.setText(now); 189 case "←" -> { 190 String inputText = inputTextField.getText(); 191 String removedText = null; 192 if (inputText != null && inputText.length() >= 1) { 193 inputTextField.setText(inputText.substring(0, inputText.length() - 1)); 194 removedText = inputText.substring(inputText.length() - 1); 195 } 196 if (!stack.isEmpty() && isRelationString(removedText)) { 197 now = stack.pop(); 198 outputTextField.setText(now); 199 } 200 } 201 case "的" -> inputTextField.setText(inputTextField.getText() + which); 202 default -> { 203 inputTextField.setText(inputTextField.getText() + which); 204 stack.push(now); 205 String result = getRelation(now, which); 206 if (isPossibleRelation(result)) { 207 result = getExactRelation(result); 208 } 209 if (result != null) { 210 now = result; 211 outputTextField.setText(now); 212 } else { 213 outputTextField.setText("超出计算范围"); 214 } 215 } 216 } 217 } 218 } 219 220 private static boolean isRelationString(String relationString) { 221 return relationString != null && "父母兄弟姐妹夫妻儿女".contains(relationString); 222 } 223 224 private static String getRelation(String now, String what) { 225 Map<String, String> relationsMap = dataMap.get(now); 226 if (relationsMap == null) { 227 return null; 228 } 229 return relationsMap.get(what); 230 } 231 232 private static boolean isPossibleRelation(String possibleRelation) { 233 return possibleRelation != null && possibleRelation.charAt(0) == '<'; 234 } 235 236 private static String getExactRelation(String possibleRelation) { 237 String[] tmp = StringUtils.substringsBetween(possibleRelation, "<", ">"); 238 if (tmp != null && tmp.length == 2) { 239 String message = "是否比"; 240 if ("伯伯".equals(tmp[0])) { 241 message += "爸爸"; 242 } else if ("姐姐".equals(tmp[0]) || "哥哥".equals(tmp[0])) { 243 message += "你"; 244 } 245 message += "大?"; 246 int userChoose = JOptionPane.showConfirmDialog(null, message, "提示", JOptionPane.YES_NO_OPTION); 247 boolean isOlder = userChoose == JOptionPane.YES_OPTION; 248 return isOlder ? tmp[0] : tmp[1]; 249 } else { 250 return null; 251 } 252 } 253 }